<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  // 关于网页中时间显示的格式  => 多种多样
  // 需求:根据用户需要的时间格式 =>返回时间

  // 2021-08-20 16:55:54
  // 2021-08-22 01:08
  // 2021-08-23
  // 09:30:25

  // "YYYY-MM-DD  hh:mm:ss"  => 2021-08-20 16:55:54
  // "YYYY-MM-DD  hh:mm"  => 2021-08-20 16:55

  var pattern = "YYYY/MM/DD  hh:mm:ss";

  var date = new Date();

  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();

  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();

  // pattern = pattern.replace("YYYY",year);  //修改之后赋值原值
  // pattern = pattern.replace("MM",beauty(month));  //修改之后赋值原值
  // pattern = pattern.replace("DD",beauty(day));  //修改之后赋值原值
  // pattern = pattern.replace("hh",beauty(hour));  //修改之后赋值原值
  // pattern = pattern.replace("mm",beauty(minute));  //修改之后赋值原值
  // pattern = pattern.replace("ss",beauty(second));  //修改之后赋值原值

  // replace的链式操作: 方法执行后  返回的也是一个字符串
  // replace()   返回的是一个新字符串  

  pattern = pattern.replace("YYYY", year).replace("MM", beauty(month)).replace("DD", beauty(day)).replace("hh", beauty(hour)).replace("mm", beauty(minute)).replace("ss", beauty(second))
  console.log(pattern);




  function beauty(num) {
    return num < 10 ? "0" + num : num;
  }



</script>

</html>